//
//  EqumentEditVC.m
//  QoilPublic
//
//  Created by SunChao on 2018/4/13.
//  Copyright © 2018年 孙超. All rights reserved.
//

#import "EqumentEditVC.h"

@interface EqumentEditVC ()<UITableViewDelegate,UITableViewDataSource>
@end

@implementation EqumentEditVC

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
     [QViewUtility setupNavigationBarBackForController:self backSelector:@selector(back)];
    self.title = @"设备编辑";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
    
    but.frame =CGRectMake(0,0, 60, 44);
    
    [but setTitle:@"保存"forState:UIControlStateNormal];
    [but setFont:[UIFont systemFontOfSize:14]];
    [but addTarget:self action:@selector(saveAction)forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem  *barBut = [[UIBarButtonItem alloc]initWithCustomView:but];
    
    self.navigationItem.rightBarButtonItem = barBut;
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.labDeviceName.text =  [self isNullToString:self.deviceName];
    self.labLicensePlate.text =  [self isNullToString:self.licensePlate];
    self.labOperatorName.text =  [self isNullToString:self.operatorName];
    self.labOperatorPhone.text = [self isNullToString:self.operatorPhone];
    [self.labDeviceName becomeFirstResponder];
    
}
- (void)saveAction{
    
    NSString *string1 =  self.labDeviceName.text;
    NSString *string2 = self.labLicensePlate.text;
    NSString *string3 = self.labOperatorName.text;
    NSString *string4 = self.labOperatorPhone.text;
    if ([string1 isEqual:@"NULL"] || [string1 isKindOfClass:[NSNull class]] || [string1 isEqual:[NSNull null]] || [string1 isEqual:NULL] || [[string1 class] isSubclassOfClass:[NSNull class]] || string1 == nil || string1 == NULL || [string1 isKindOfClass:[NSNull class]] || [[string1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0 || [string1 isEqualToString:@"<null>"] || [string1 isEqualToString:@"(null)"]) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"名称不能为空"
                                                                       message:@""
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action) {
                                                                 //响应事件
                                                                 NSLog(@"action = %@", action);
                                                             }];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:nil];
        return;
    }
    if ([string2 isEqual:@"NULL"] || [string2 isKindOfClass:[NSNull class]] || [string2 isEqual:[NSNull null]] || [string2 isEqual:NULL] || [[string2 class] isSubclassOfClass:[NSNull class]] || string2 == nil || string2 == NULL || [string2 isKindOfClass:[NSNull class]] || [[string2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0 || [string2 isEqualToString:@"<null>"] || [string2 isEqualToString:@"(null)"]) {
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"车牌号不能为空"
                                                                       message:@""
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action) {
                                                                 //响应事件
                                                                 NSLog(@"action = %@", action);
                                                             }];
        [alert addAction:cancelAction];
         [self presentViewController:alert animated:YES completion:nil];
    }
    if ([string3 isEqual:@"NULL"] || [string3 isKindOfClass:[NSNull class]] || [string3 isEqual:[NSNull null]] || [string3 isEqual:NULL] || [[string3 class] isSubclassOfClass:[NSNull class]] || string3 == nil || string3 == NULL || [string3 isKindOfClass:[NSNull class]] || [[string3 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0 || [string3 isEqualToString:@"<null>"] || [string3 isEqualToString:@"(null)"]) {
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"司机不能为空"
                                                                       message:@""
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action) {
                                                                 //响应事件
                                                                 NSLog(@"action = %@", action);
                                                             }];
        [alert addAction:cancelAction];
         [self presentViewController:alert animated:YES completion:nil];
        return;
    }
    if ([string4 isEqual:@"NULL"] || [string4 isKindOfClass:[NSNull class]] || [string4 isEqual:[NSNull null]] || [string4 isEqual:NULL] || [[string4 class] isSubclassOfClass:[NSNull class]] || string4 == nil || string4 == NULL || [string4 isKindOfClass:[NSNull class]] || [[string4 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0 || [string4 isEqualToString:@"<null>"] || [string4 isEqualToString:@"(null)"]) {
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"电话不能为空"
                                                                       message:@""
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action) {
                                                                 //响应事件
                                                                 NSLog(@"action = %@", action);
                                                             }];
        [alert addAction:cancelAction];
         [self presentViewController:alert animated:YES completion:nil];
        return;
    }
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"确定保存？"
                                                                   message:@""
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              //响应事件
                                                              NSLog(@"action = %@", action);
                                                          }];
    
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * action) {
                                                             //响应事件
                                                             NSLog(@"action = %@", action);
                                                              [self requestData];
                                                         }];
    [alert addAction:defaultAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
   

}


- (void)requestData{
    
    NSString *url = [QParamService urlWithIOTAction:@"userDevice/updateUserDevice"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:[QParamService getToken] forKey:@"token"];
    [param setObject:self.deviceId forKey:@"id"];
    [param setObject:self.labDeviceName.text forKey:@"deviceName"];
    [param setObject:self.labOperatorName.text forKey:@"operatorName"];
    [param setObject:self.labOperatorPhone.text forKey:@"operatorPhone"];
    [param setObject:self.labLicensePlate.text forKey:@"licensePlate"];
    
    
    [[QHttpManager sharedManager] POST:url parameterMD5jm:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if (error) {
            
            NSLog(@"error:%@",basModel.errMsg);
            
            return ;
        }
        
        if([basModel.result isEqualToString:@"success"]){
           
            [self.navigationController popViewControllerAnimated:YES];
            
        }
    }];
}

#pragma mark -字符串判空
- (NSString *)isNullToString:(id)string
{
    if ([string isEqual:@"NULL"] || [string isKindOfClass:[NSNull class]] || [string isEqual:[NSNull null]] || [string isEqual:NULL] || [[string class] isSubclassOfClass:[NSNull class]] || string == nil || string == NULL || [string isKindOfClass:[NSNull class]] || [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0 || [string isEqualToString:@"<null>"] || [string isEqualToString:@"(null)"])
    {
        return @"";
        
        
    }else
    {
        
        return (NSString *)string;
    }
}
@end
